package com.beamfield.controller.manage.device;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.*;
import com.beamfield.service.manage.device.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 设备进场管理
 */
@Controller
@RequestMapping("/cms/device/inbound")
public class DevInboundController extends BaseController {

    @Value("${session.beam}")
    private String beam;
    @Value("${session.user}")
    private String user;

    @RequestMapping(value = "/inbound")
    public ModelAndView inbound(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/inbound_detail")
    public ModelAndView inboundDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/inbound_new")
    public ModelAndView inboundNew(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/inbound_edit")
    public ModelAndView inbound_edit(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/debug")
    public ModelAndView debug(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/debugDetail")
    public ModelAndView debugDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/workDetail")
    public ModelAndView workDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/inspectDetail")
    public ModelAndView inspectDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/maintain")
    public ModelAndView maintain(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/maintainDetail")
    public ModelAndView maintainDetail(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/deviceChange")
    public ModelAndView deviceChange(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/device_source")
    public ModelAndView device_source(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/check")
    public ModelAndView check(){
        return new ModelAndView();
    }
    @Autowired
    private DeviceService deviceService;

    /**
     * 保存采购计划
     * @param inboundJson
     * @param device_standard
     * @param device_name
     * @param type
     * @param session
     * @return
     */
    @RequestMapping("/saveInbound")
    @ResponseBody
    public ApiResult saveInbound(String inboundJson,String device_standard,String device_name,int type,HttpSession session){

        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        try {
            DeviceInbound deviceInbound = JSON.parseObject(inboundJson, DeviceInbound.class);
            ApiResult apiResult= deviceService.saveInbound(deviceInbound,beamId,userId,device_standard,device_name,type);
            return  apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存采购计划
     * @param inboundJson
     * @return
     */
    @RequestMapping("/editInbound")
    @ResponseBody
    public ApiResult editInbound(String inboundJson, HttpSession session){

        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        try {
            DeviceInbound deviceInbound = JSON.parseObject(inboundJson, DeviceInbound.class);
            if(deviceInbound == null || deviceInbound.getId() == null){
                return new ApiResult(HttpResultEnum.MIS_PARAM);
            }
            ApiResult apiResult= deviceService.editInbound(deviceInbound,beamId,userId);
            return  apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存设备入库验收信息
     * @param inboundId
     * @param checkDate
     * @param debug
     * @param debugDetail
     * @return
     */
    @RequestMapping("/saveInboundCheck")
    @ResponseBody
    public ApiResult saveInboundCheck(Long inboundId,String checkDate,Integer debug,String debugDetail,HttpSession session){
        try {
            return deviceService.saveInboundCheck(inboundId,checkDate,debug,debugDetail);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**获取采购计划
     * @param session
     * @param plan_name
     * @param type
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping("/getInbound")
    @ResponseBody
    public  PageResult getInbound(HttpSession session,String plan_name,Integer type,String offset,String limit,String use_unit,String devicename){
        long beamId = (long) session.getAttribute(this.beam);
        try {
            PageResult inbounds=deviceService.getInBoundList(beamId,plan_name,type,offset,limit,use_unit,devicename);
            return  inbounds;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**保存试运行记录
     * @param session
     * @param debugJson
     * @return
     */
    @RequestMapping("/saveDebug")
    @ResponseBody
    public ApiResult saveDebug(HttpSession session,String debugJson){
        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        ApiResult apiResult;
        try {
            DeviceDebugRecord deviceDebugRecord = JSON.parseObject(debugJson, DeviceDebugRecord.class);
            apiResult = deviceService.saveDebug(deviceDebugRecord, beamId, userId);
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }
    /**保存运转记录
     * @param session
     * @param workJson
     * @return
     */
    @RequestMapping("/saveWork")
    @ResponseBody
    public ApiResult saveWork(HttpSession session,String workJson){
        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        ApiResult apiResult;
        try {
            DeviceWork deviceWork = JSON.parseObject(workJson, DeviceWork.class);
            apiResult = deviceService.saveWork(deviceWork, beamId, userId);
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }

    /**
     * 保存检查记录
     * @param session
     * @param saveInspect
     * @return
     */
    @RequestMapping("/saveInspect")
    @ResponseBody
    public ApiResult saveInspect(HttpSession session,String saveInspect){
        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        ApiResult apiResult;
        try {
            DeviceInspect deviceInspect = JSON.parseObject(saveInspect, DeviceInspect.class);
            apiResult = deviceService.saveInspect(deviceInspect, beamId, userId);
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }

    /**
     * 保存保养记录
     * @param session
     * @param maintainJson
     * @return
     */
    @RequestMapping("/saveMaintain")
    @ResponseBody
    public ApiResult saveMaintain(HttpSession session,String maintainJson){
        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        ApiResult apiResult;
        try {
            DeviceMaintain deviceMaintain = JSON.parseObject(maintainJson, DeviceMaintain.class);
            apiResult = deviceService.saveMaintain(deviceMaintain, beamId, userId);
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }


    /**
     * 获取试运行记录
     * @param inbound_id
     * @return
     */
    @RequestMapping("/getDebug")
    @ResponseBody
    public ApiResult getDebug(long inbound_id){
        try {
            ApiResult inBoundById = deviceService.getInBoundById(inbound_id);
            return inBoundById;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }

    /**
     * 获取保养记录
     * @param inbound_id
     * @return
     */
    @RequestMapping("/getMaintainDetail")
    @ResponseBody
    public PageResult getMaintainDetail(long inbound_id, long device_id, Long offset, int limit, String maintain_type, String start_date, HttpSession session){
        long beamId = (long) session.getAttribute(this.beam);
        try {
            List<DeviceMaintain> maintainById = deviceService.getMaintainByInboundId(inbound_id, device_id, offset,limit,maintain_type,start_date,beamId);
            long maintainNums = deviceService.getMaintainNums(inbound_id, device_id, maintain_type, start_date);
            return new PageResult(maintainNums,offset,maintainById);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 获取运转记录
     * @param inbound_id
     * @return
     */
    @RequestMapping("/getWork")
    @ResponseBody
    public PageResult getWork(long inbound_id,long device_id,long offset,int limit){
        try {
            PageResult workById = deviceService.getWorkById(inbound_id, device_id,offset,limit);
            return workById;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 获取检查记录
     * @param inbound_id
     * @return
     */
    @RequestMapping("/getInspect")
    @ResponseBody
    public PageResult getInspect(long inbound_id,long device_id, String offset, String limit){
        try {
            List<DeviceInspect> workById = deviceService.getInspectById(inbound_id, device_id, offset, limit);
            long total = deviceService.countInspectById(inbound_id, device_id);
            return new PageResult(total, CommonUtils.parseLong(offset, 0L), workById);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 删除试运行记录
     * @param id
     * @return
     */
    @RequestMapping("/delInbound")
    @ResponseBody
    public ApiResult delInbound(long id){
        try {
            ApiResult apiResult = deviceService.delInbound(id);
            return apiResult;
       }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }

    /**
     * 获取设备入库数及采购数
     * @param session
     * @param type
     * @return ApiResult("planNums,inNum")
     */
    @RequestMapping("/getInboundNum")
    @ResponseBody
    public ApiResult getInboundNum(HttpSession session,int type){
        long beamId = (long) session.getAttribute(this.beam);
        try {
            ApiResult apiResult = deviceService.getInboundNum(beamId,type);
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }
    }
    /**
     * 根据设备id获取设备信息
     * @return
     */
    @RequestMapping(value = "/getDeviceInfoById")
    @ResponseBody
    public ApiResult getDeviceInfoById(String device_id){
        try {
            ApiResult apiResult =  new ApiResult(deviceService.getDeviceInfoByInboundId(Long.parseLong(device_id)));
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }

    }

    @RequestMapping(value = "/inboundWithGPS")
    public ModelAndView inboundWithGPS(){
        return new ModelAndView();
    }

    /**
     * 获取GPS设备列表
     * @return
     */
    @RequestMapping("/getInboundWithGPSList")
    @ResponseBody
    public PageResult getInboundWithGPSList(HttpServletRequest request, String offset, String limit){
        Long beamId = super.getBeamId(request);
        try {
            return deviceService.getInboundWithGPSList(Integer.parseInt(offset),Integer.parseInt(limit),beamId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 编辑GPS
     * @return
     */
    @RequestMapping(value = "/doEditGPS")
    @ResponseBody
    public ApiResult doEditGPS(HttpServletRequest request,Long inboundId,Integer gpsNo,String type){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);

        return deviceService.editGPS(inboundId,gpsNo,beamId,userId,type);
    }
    /**
     * 获取设备列表
     * @return
     */
    @RequestMapping("/getAllInbound")
    @ResponseBody
    public List<DeviceInbound> getAllInbound(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        try {
            return deviceService.getAllInbound(beamId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 保存设备变动记录
     * @return
     */
    @RequestMapping(value = "/saveDeviceChange")
    @ResponseBody
    public ApiResult saveDeviceChange(HttpServletRequest request,Long inboundId,Long acBeamId){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);

        return deviceService.saveDeviceChange(inboundId,acBeamId,beamId,userId);
    }

    /**
     * 获取设备变动记录列表
     * @return
     */
    @RequestMapping(value = "/getDeviceChangeList")
    @ResponseBody
    public PageResult getDeviceChangeList(HttpServletRequest request,String deviceName, Integer offset, Integer limit){
        Long beamId = super.getBeamId(request);

        return deviceService.getDeviceChangeList(deviceName,offset,limit,beamId);
    }

    /**
     * 根据类型获取设备名称集合
     * @return
     */
    @RequestMapping(value = "/getDeviceNamesByType")
    @ResponseBody
    public ApiResult getDeviceNamesByType(HttpServletRequest request,Integer type){
        Long beamId = super.getBeamId(request);

        return new ApiResult(deviceService.getDeviceNamesByType(type,beamId));
    }

    /**
     * 根据类型和名称获取设备管理编号集合
     * @return
     */
    @RequestMapping(value = "/getManagerNumByTypeAndDeviceName")
    @ResponseBody
    public ApiResult getManagerNumByTypeAndDeviceName(HttpServletRequest request,Integer type,String deviceName){
        Long beamId = super.getBeamId(request);

        return new ApiResult(deviceService.getManagerNumByTypeAndDeviceName(type,deviceName,beamId));
    }

    /**
     * 根据设备管理编号获取入库信息
     * @return
     */
    @RequestMapping(value = "/getInboundByManagerNum")
    @ResponseBody
    public ApiResult getInboundByManagerNum(HttpServletRequest request,String managerNum){
        Long beamId = super.getBeamId(request);

        return new ApiResult(deviceService.getInboundByManagerNum(managerNum));
    }

    /**
     * 根据设备管理编号获取入库信息
     * @return
     */
    @RequestMapping(value = "/getInboundById")
    @ResponseBody
    public ApiResult getInboundById(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM);
        }
        return new ApiResult(deviceService.getInboundById(id));
    }
}
